package br.com.lis;

import java.util.List;

import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentTransaction;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import br.com.lis.exercicio.ExercicioDbHelper;
import br.com.lis.exercicio.Question;

public class ExercicioActivity extends  Fragment {
	Context ctx;
	
	List<Question> quesList;
	int score=0;
	int qid=0;
	Question currentQ;
	TextView txtQuestion;
	RadioButton rda, rdb, rdc;
	Button butNext;
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		final View view = inflater.inflate(R.layout.activity_exercicio, null);
		
		ctx = getActivity().getApplicationContext();
		final String curso = getArguments().getString("curso");  
		final String aula = getArguments().getString("aula");
		
		ExercicioDbHelper db=new ExercicioDbHelper(ctx);
		quesList=db.getSpecificQuestions(curso, aula);
		/*if (curso.equals("1") && aula.equals("1")){ 
		quesList=db.getAlgAul1Questions();
		}
		
		if (curso.equals("1") && aula.equals("2")){ 
			quesList=db.getAlgAul2Questions();
		}*/
		
		currentQ=quesList.get(qid);
		txtQuestion=(TextView) view.findViewById(R.id.textView1);
		rda=(RadioButton)view.findViewById(R.id.radio0);
		rdb=(RadioButton)view.findViewById(R.id.radio1);
		rdc=(RadioButton)view.findViewById(R.id.radio2);
		butNext=(Button)view.findViewById(R.id.button1);
		setQuestionView();
		butNext.setOnClickListener(new View.OnClickListener() {		
			@Override
			public void onClick(View v) {
				RadioGroup grp=(RadioGroup) view.findViewById(R.id.radioGroup1);
				RadioButton answer=(RadioButton)view.findViewById(grp.getCheckedRadioButtonId());
				Log.d("resposta", currentQ.getANSWER()+" vs "+answer.getText());
				if(currentQ.getANSWER().equals(answer.getText()))
				{
					score++;
					Log.d("score", "Your score "+score);
				}
				if(qid<5){					
					currentQ=quesList.get(qid);
					setQuestionView();
				}else{
					FragmentTransaction ft;
		    		Fragment frag;
		    		Bundle b = new Bundle();
					b.putInt("score", score);
					b.putString("curso", curso);
					b.putString("aula", aula);
					frag = new ExercicioResultadoActivity();
					frag.setArguments(b);
					ft = getFragmentManager().beginTransaction();					
					ft.replace(R.id.flDrawerHome, frag);
					ft.commit();
				}
			}
		});
		
		return(view);
	}
	
	private void setQuestionView()
	{
		txtQuestion.setText(currentQ.getQUESTION());
		rda.setText(currentQ.getOPTA());
		rdb.setText(currentQ.getOPTB());
		rdc.setText(currentQ.getOPTC());
		qid++;
	}
}
